package com.android.likeweixin.asynctask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.android.likeweixin.model.FriendDynamic;
import com.android.likeweixin.model.FriendDynamic.Comment;
import com.android.likeweixin.model.FriendDynamic.Praise;

public class LoadFriendDynamicTask extends BaseAsyncTask {

	public LoadFriendDynamicTask(Context context, int type) {
		super(context, type);
	}

	@Override
	protected Map<String, Object> doInBackground(Object... params) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		//makeData(jsonStr, dataMap);
		// test data
		List<FriendDynamic> fds = new ArrayList<FriendDynamic>();
		 FriendDynamic fd = null;
		 fd = new FriendDynamic();
		 fd.realName = "张三1";
		 fd.newsContent = "但是发生发生";
		 List<String> pics = new ArrayList<String>();
		 pics.add("图片地址1");
		 fd.pics = pics;
		 fds.add(fd);
		 // 测试赞
		 fd = new FriendDynamic();
		 fd.realName = "张三2";
		 fd.newsContent = "fdsafsfdafas";
		 List<Praise> prs = new ArrayList<Praise>();
		 Praise p = new Praise();
		 p.userId = "李四id";
		 p.realName = "李四";
		 prs.add(p);
		 p = new Praise();
		 p.userId = "李四id1";
		 p.realName = "李四1";
		 prs.add(p);
		 fd.mPraises = prs;
		 // 测试评论
		 List<Comment> cts = new ArrayList<Comment>();
		 Comment ct = new Comment();
		 ct.realName = "李四";
		 ct.commentContent = "不错不错";
		 cts.add(ct);
		 ct = new Comment();
		 ct.realName = "李四1";
		 ct.commentContent = "不错不错1";
		 cts.add(ct);
		 ct = new Comment();
		 ct.realName = "李四2";
		 ct.replyRealName = "张三";
		 ct.commentContent = "不错不错2[new_expression_frame_001]";
		 cts.add(ct);
		 fd.mComments = cts;
		 fds.add(fd);
		 //
		 fd = new FriendDynamic();
		 fd.realName = "张三3";
		 pics = new ArrayList<String>();
		 pics.add("图片地址1");
		 pics.add("图片地址2");
		 pics.add("图片地址3");
		 pics.add("图片地址4");
		 fd.pics = pics;
		 fds.add(fd);
		 for (int i = 0; i < 6; i++) {
		 fd = new FriendDynamic();
		 fd.realName = "李四" + i;
		 fd.newsContent = "但是发生发生" + i;
		 fds.add(fd);
		 }
		 dataMap.put("data", fds);
		return dataMap;
	}

	private Map<String, Object> makeData(String jsonStr,
			Map<String, Object> dataMap) {
		try {
			JSONObject instance = new JSONObject(jsonStr);
			JSONArray array = instance.getJSONArray("news");
			dataMap.put("curTime", instance.getString("curTime"));
			List<FriendDynamic> fds = new ArrayList<FriendDynamic>();
			FriendDynamic fd;
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj = array.getJSONObject(i);
				fd = new FriendDynamic();
				fd.newsId = obj.getString("newsId");
				fd.newsContent = obj.getString("newsContent");
				fd.realName = obj.getString("realName");
				fd.useId = obj.getString("userId");
				fd.newsTime = obj.getLong("newsTime");
				// 赞
				List<Praise> praises = makePraise(obj.getJSONArray("praise"));
				fd.mPraises = praises;
				// 评论
				List<Comment> comms = makeComment(obj.getJSONArray("comment"));
				fd.mComments = comms;
				// 图片
				List<String> pics = makePics(obj.getJSONArray("pics"));
				fd.pics = pics;
				fds.add(fd);
			}
			dataMap.put("data", fds);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return dataMap;
	}

	private List<Praise> makePraise(JSONArray praiseArray) {
		List<Praise> praises = new ArrayList<Praise>();
		Praise p;
		try {
			for (int i = 0; i < praiseArray.length(); i++) {
				JSONObject obj = praiseArray.getJSONObject(i);
				p = new Praise();
				p.realName = obj.getString("realName");
				p.userId = obj.getJSONObject("userPraiseId")
						.getString("userId");
				praises.add(p);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return praises;
	}

	private List<Comment> makeComment(JSONArray commArray) {
		List<Comment> comments = new ArrayList<Comment>();
		Comment c;
		try {
			for (int i = 0; i < commArray.length(); i++) {
				JSONObject obj = commArray.getJSONObject(i);
				c = new Comment();
				c.commentId = obj.getString("commentId");
				c.commentContent = obj.getString("commentContent");
				c.newsId = obj.getString("newsId");
				//回复者
				c.userId = obj.getString("userId");
				c.realName = obj.getString("realName");
				//被回复者
				c.replyUserId = obj.getString("replyUserId");
				c.replyUserId = c.replyUserId.equals("null") ? "" : c.replyUserId;
				c.replyRealName = obj.getString("replyRealName");
				c.replyRealName = c.replyRealName.equals("null") ? "" : c.replyRealName;
				comments.add(c);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return comments;
	}
	private List<String> makePics(JSONArray picArray) {
		List<String> pics = new ArrayList<String>();
		String pic;
		try {
			for (int i = 0; i < picArray.length(); i++) {
				JSONObject obj = picArray.getJSONObject(i);
				obj = obj.getJSONObject("newsPicId");
				pic = obj.getString("newsPic");
				pics.add(pic);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return pics;
	}
}
